package org.acy.file.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.acy.file.domain.CottonFileInfo;
import org.acy.file.domain.CottonFileInfoError;
import org.acy.file.domain.CottonFileInfoNew;
import org.acy.file.mapper.CottonFileInfoErrorMapper;
import org.acy.file.mapper.CottonFileInfoMapper;
import org.acy.file.mapper.CottonFileInfoNewMapper;
import org.acy.file.service.ICottonFileInfoService;
import org.acy.file.util.DateUtil;
import org.acy.file.util.FastDfsClientUtil;
import org.acy.file.util.HttpUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service
@Slf4j
public class CottonFileInfoServiceImpl extends ServiceImpl<CottonFileInfoMapper, CottonFileInfo>
        implements ICottonFileInfoService {

    @Resource
    FastDfsClientUtil fastDfsClientUtil;

    @Resource
    CottonFileInfoNewMapper cottonFileInfoNewMapper;

    @Resource
    CottonFileInfoErrorMapper cottonFileInfoErrorMapper;

    @Value("${fdfs.web-server-url}")
    private String IMAGE_SERVER_PATH;

    public static final String MIN_DATE = "2024-06-14";
    public static final String END_DATE = "2024-07-04";

    /**
     * 转存数据
     *
     * @param cottonFileInfo
     */
    private void createNew(CottonFileInfo cottonFileInfo) {
        try {
            CottonFileInfoNew res = new CottonFileInfoNew();
            BeanUtils.copyProperties(cottonFileInfo, res);
            String loadUrl = "http://192.168.100.55:8818/dfs/" + cottonFileInfo.getFdfsFileId();
            byte[] bytes = HttpUtil.read(loadUrl);
            String fileId = fastDfsClientUtil.uploadOriginal(bytes, cottonFileInfo.getExtName());
            res.setLoadUrl(IMAGE_SERVER_PATH + fileId);
            res.setFdfsFileId(fileId);
            cottonFileInfoNewMapper.insert(res);
        } catch (Exception e) {
            log.error("转存异常", e);
            CottonFileInfoError error = new CottonFileInfoError();
            BeanUtils.copyProperties(cottonFileInfo, error);
            cottonFileInfoErrorMapper.insert(error);
        }

    }

    @Override
    public void toNewService() {
        Date date = DateUtil.getFormatDate(MIN_DATE);
        Date end = DateUtil.getFormatDate(END_DATE);
        while (date.getTime() < end.getTime()) {
            List<CottonFileInfo> infoList = queryFileInfo(date);
            if (!CollectionUtils.isEmpty(infoList)) {
                for (CottonFileInfo cottonFileInfo : infoList) {
                    createNew(cottonFileInfo);
                }
            }
            date = DateUtil.getAfterDay(date, 1);
        }
    }

    @Override
    public void errorFileService() {
        List<CottonFileInfoError> list = cottonFileInfoErrorMapper.selectList(new QueryWrapper<>());
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (CottonFileInfoError error : list) {
            try {
                CottonFileInfoNew res = new CottonFileInfoNew();
                BeanUtils.copyProperties(error, res);
                String loadUrl = "http://192.168.100.55:8818/dfs/" + error.getFdfsFileId();
                //String loadUrl = "https://fast.cottech.com/dfs/" + error.getFdfsFileId();
                byte[] bytes = HttpUtil.read(loadUrl);
                String fileId = fastDfsClientUtil.uploadOriginal(bytes, error.getExtName());
                res.setLoadUrl(IMAGE_SERVER_PATH + fileId);
                res.setFdfsFileId(fileId);
                cottonFileInfoNewMapper.insert(res);
                cottonFileInfoErrorMapper.deleteById(error.getId());
            } catch (Exception e) {
                log.error("异常文件处理错误", e);
            }
        }
    }


    private List<CottonFileInfo> queryFileInfo(Date date) {
        QueryWrapper<CottonFileInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("EXP_TIME", new Date());
        queryWrapper.between("create_time", DateUtil.getZeroDate(date), DateUtil.getNightDate(date));
        return this.list(queryWrapper);
    }


    public static void main(String[] args) {
        Date date = DateUtil.getFormatDate("2024-06-13");
        date = DateUtil.getAfterDay(date, 1);
        Date end = DateUtil.getFormatDate("2024-06-14");
        System.out.println(date.getTime() < end.getTime());
    }
}


